package app

import (
	"github.com/go-redis/redis"
	"strings"
)

var (
	IsCluster     bool
	client        *redis.Client
	clusterClient *redis.ClusterClient
	err           error
)

type Options struct {
	Addr string
}

func GetRedisClient() *redis.Client {
	return client
}

func InitRedis(isCluster bool, addrs string) {
	IsCluster = isCluster
	if IsCluster {
		clusterClient = redis.NewClusterClient(&redis.ClusterOptions{
			// Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
			Addrs: strings.Split(addrs, ","),
		})
		_, err := clusterClient.Ping().Result()
		if err != nil {
			panic(err)
		}
	} else {
		client = redis.NewClient(&redis.Options{
			Addr: addrs,
		})

		_, err := client.Ping().Result()
		if err != nil {
			panic(err)
		}
	}
}
